﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Resources;
    using System.Text;
    using System.Windows.Data;
    using ReflectionStudio.Core.Interface;
    using ReflectionStudio.Core.Events;
    /// <summary>
    /// Represents a static resource.
    /// </summary>
    internal class StaticResource : IResource
    {
        //Constructors

        /// <summary>
        /// Creates a new instance of the <see cref="StaticResource"/> class.
        /// Assigns the <paramref name="key"/> parameter at the property Key and the <paramref name="resourceType"/> parameter at the property ResourceType.
        /// </summary>
        /// <param name="key">The key of the resource.</param>
        /// <param name="resourceType">Specifies the resource's type.</param>
        /// <exception cref="ArgumentException">
        /// Thrown whether <paramref name="key"/> parameter is either null or equals to white space.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown whether <paramref name="resourceType"/> parameter is null.
        /// </exception>
        public StaticResource(String key, Type resourceType)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentException("key parameter shouldn't be either null or equals at white space.");
            }
            if (resourceType == null)
            {
                throw new ArgumentNullException("resourceType parameter shouldn't be null.");
            }
            this.Key = key;
            this.ResourceType = resourceType;
        }

        //Properties

        /// <summary>
        /// Gets the resource's key.
        /// </summary>
        public String Key { get; private set; }

        /// <summary>
        /// Gets the resource's type.
        /// </summary>
        public Type ResourceType { get; private set; }

        //Methods

        /// <summary>
        /// Serves as a hash function for <see cref="StaticResource"/> type.
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Key.GetHashCode();
        }

        /// <summary>
        /// Determines whether the specified <see cref="StaticResource"/> is equals to the current <see cref="StaticResource"/>.
        /// </summary>
        /// <param name="obj">Instance.</param>
        /// <returns>True whether the input instance is equals to the current instance otherwise false.</returns>
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return true;
            }
            else if ((obj == null) || (obj.GetType() != this.GetType()))
            {
                return false;
            }
            StaticResource instance = obj as StaticResource;
            return ((instance.Key.Equals(this.Key)) && 
                (instance.ResourceType.AssemblyQualifiedName.Equals(this.ResourceType.AssemblyQualifiedName)));
        }

        /// <summary>
        /// Gets localized text.
        /// </summary>
        /// <returns></returns>
        public Object GetLocalizedValue()
        {
            return null;
            try
            {
                /*
                Assembly assembly = this.ResourceType.Assembly;
                ResourceManager resourceManager = new ResourceManager(this.ResourceType.FullName, assembly);
                return resourceManager.GetString(this.Key);*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace,ex);
                return null;
            }
        }
    }
}